package com.ifnk.portaljava.hlyt.etl;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;


public class timerFile {
	Timer timer; 
	File dataDir;
	Properties prop;
	public timerFile(int seconds) { 
		timer = new Timer(); 
		prop = new Properties();
		try {
			prop.load(timerFile.class.getResourceAsStream("etl.properties"));

			dataDir = new File(prop.getProperty("dataDir"));

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		RemindTask rt = new RemindTask(dataDir);
		timer.schedule(rt, seconds*100,5000); 
	} 

	class RemindTask extends TimerTask { 
		File _dataDir;
		Properties _prop;
		String fn;
		Date dt;
		int i = 0;
		int k = 100;
		int k1 = 110;
		int k2 = 120;
		int k3 = 130;
		double k4 = 100.0;
		double k5 = 110.0;
		double k6 = 120.0;
		double k7 = 130.0;
		public RemindTask(File dd ){
			_dataDir =dd;
			dt = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			fn = sdf.format(dt);
	        
		}

		public  void productFile(String logFileName,String content) {
			try{
				File log = new File(logFileName);
				if (log.length() < 102400) {
					FileWriter fw = new FileWriter(logFileName);
					 BufferedWriter bw = new BufferedWriter(fw);    
//				?	RandomAccessFile raf = new RandomAccessFile(logFileName, "rw");
//					String content = "1234";
//					String contents = sdf.format(new Date()) + content + "\r\n";
					bw.write(content); //写入文件
	                bw.newLine();
	                bw.flush();    //刷新该流的缓冲
	                bw.close();
	                fw.close();

				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		public void run() { 
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			dt.setTime(dt.getTime()+60000);
			 fn = sdf.format(dt);
			 System.out.println(_dataDir.getPath()+"\\gndata_"+fn+".txt "+i);
			 k +=(int) (Math.random() * 100);
			 k1 +=(int) (Math.random() * 100);
			 k2 +=(int) (Math.random() * 100);
			 k3 +=(int) (Math.random() * 100);
			 k4 +=(Math.random() * 100)-(Math.random() * 100);
			 k5 +=(Math.random() * 100)-(Math.random() * 100);
			 k6 +=(Math.random() * 100)-(Math.random() * 100);
			 k7 +=(Math.random() * 100)-(Math.random() * 100);
			 String content = fn+","+k+","+k1+","+k2+","+k3+","+k4+","+k5+","+k6+","+k7;
			productFile(_dataDir.getPath()+"\\gndata_"+fn+".csv",content);
//			productFile(_dataDir.getPath()+"\\gnflow_"+fn+".csv",content);
			i++;
			if(i>30){
				System.out.println("Time's up!"); 
				timer.cancel(); //Terminate the timer thread 
			}
		} 

	} 

	public static void main(String args[]) { 
		System.out.println("About to schedule task."); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date dt = new Date();
		String fn = sdf.format(dt);
		System.out.println(fn);
		dt.setTime(dt.getTime()+60000);
		 fn = sdf.format(dt);
		System.out.println(fn);
		
		new timerFile(1); 
		System.out.println("Task scheduled."); 
	} 

}
